package com.zjtaixue.o2o.service.impl;

import com.zjtaixue.o2o.dto.ShopExecution;
import com.zjtaixue.o2o.entity.Shop;
import com.zjtaixue.o2o.enums.ShopEnableStatus;
import com.zjtaixue.o2o.enums.ShopStatusEnum;
import com.zjtaixue.o2o.mapper.ShopMapper;
import com.zjtaixue.o2o.service.ShopService;
import com.zjtaixue.o2o.util.ImageUtil;
import com.zjtaixue.o2o.util.PageUtil;
import com.zjtaixue.o2o.util.PathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    /**
     * 图片路径
     * @param shop
     * @param shopImg
     * @return
     */
    @Override
    @Transactional
    public ShopExecution addShop(Shop shop, InputStream shopImg,String fileName) {
        //空值判断
        if(shop==null){
            return new ShopExecution(ShopStatusEnum.NULL_SHOP);
        }try{
            //给店铺信息赋值初始值
            shop.setEnableStatus(ShopEnableStatus.CHECK.getCode());//店铺审核中
            shop.setCreateTime(new Date());
            shop.setLastEditTime(new Date());
            //添加店铺信息
            Shop result= shopMapper.selectByShopName(shop.getShopName());
            if(result!=null){
                return new ShopExecution(ShopStatusEnum.SHOP_NAME_IS_RIGISTER);
            }
            int effectNum=shopMapper.insert(shop);
            if(effectNum<=0){
                return new ShopExecution(ShopStatusEnum.SHOP_RIGITER_FAILD);
            }else{
                if(shopImg!=null){
                    //存储图片
                    try{
                        addShopImg(shop,shopImg,fileName);
                    }catch (Exception e){
                        return new ShopExecution(ShopStatusEnum.SHOP_IMAGE_INSERT_FAILD);
                    }
                    //更新店铺的图片地址
                    effectNum=shopMapper.updateShop(shop);
                    if(effectNum<0){
                        return new ShopExecution(ShopStatusEnum.SHOP_IMAGE_ADDRESS_UPDATE_FAILD);
                    }
                }
            }
        }catch (Exception e){
            throw new RuntimeException("addShop error:"+e.getMessage());
        }
        return new ShopExecution(ShopStatusEnum.CHECK,shop);
    }

    @Override
    public Shop getByShopId(long shopId) {
        return shopMapper.selectByShopId(shopId);
    }

    @Override
    public ShopExecution modifyShop(Shop shop, InputStream shopImg, String fileName) {
        if(shop==null||shop.getShopId()==null){
            return new ShopExecution(ShopStatusEnum.NULL_SHOP);
        }else{
            try{
                //1.判断是否需要处理图片
                if(shopImg!=null&&fileName!=null&&!"".equals(fileName)){
                    Shop tempShop=shopMapper.selectByShopId(shop.getShopId());
                    if(tempShop.getShopImg()!=null){
                        ImageUtil.deleteFileOrPath(tempShop.getShopAddress());
                    }
                    addShopImg(shop,shopImg,fileName);
                }
                //2.更新店铺信息
                shop.setLastEditTime(new Date());
                int effectNum=shopMapper.updateShop(shop);
                if(effectNum<=0){
                    return new ShopExecution(ShopStatusEnum.SHOP_OPERATION_ERROR);
                }else {
                    shop=shopMapper.selectByShopId(shop.getShopId());
                    return new ShopExecution(ShopStatusEnum.SUCCESS,shop);
                }
            }catch (Exception e){
                return new ShopExecution(ShopStatusEnum.SHOP_OPERATION_ERROR);
            }
        }
    }



    private void addShopImg(Shop shop, InputStream shopImg,String fileName){
        //获取shop图片目录的相对值路径
        String dest= PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddress= ImageUtil.generateThumbnail(shopImg,fileName,dest);
        log.debug("更新的图片地址是={}",shopImgAddress);
        shop.setShopImg(shopImgAddress);
    }

    @Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        int rowIndex= PageUtil.calculateRowIndex(pageIndex,pageSize);
        List<Shop> shopList=shopMapper.selectShopListByCondition(shopCondition,rowIndex,pageSize);
        int count=shopMapper.selectShopCountByCondition(shopCondition);
        if(shopList!=null){
            return new ShopExecution(count,shopList,ShopStatusEnum.SUCCESS);
        }else{
            return new ShopExecution(ShopStatusEnum.NULL_SHOP);
        }
    }
}















